package com.ruoyi.ai.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.ai.service.IExtendUserService;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description:LoginController
 * @author: caoes
 * create:2023/6/28 14:36
 **/
@Api(tags = "注册登陆相关接口")
@RestController
public class LoginController {

    @Autowired
    private IExtendUserService iExtendUserService;

    @ApiOperation(value = "sendSmsCode", notes = "发送短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, dataType = "String")
    })
    @PostMapping(value = "sendSmsCode")
    public AjaxResult sendSmsCode(HttpServletRequest request, String mobile) {
        if (StrUtil.isBlank(mobile)){
            return AjaxResult.error("手机号不可为空");
        }
        if (!Validator.isMobile(mobile)) {
            return AjaxResult.error("手机号码输入不正确");
        }
        iExtendUserService.sendSmsCode(request, mobile);
        return AjaxResult.success("发送成功");
    }
    @ApiOperation(value = "smsLogin", notes = "手机号码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "validCode", value = "验证码", required = true, dataType = "String")
    })
    @PostMapping(value = "smsLogin")
    public AjaxResult smsLogin(HttpServletRequest request, HttpSession httpSession, String mobile, String validCode) {
        if (StrUtil.isBlank(mobile)){
            return AjaxResult.error("手机号不可为空");
        }
        if (!Validator.isMobile(mobile)) {
            return AjaxResult.error("手机号码输入不正确");
        }
        if (StrUtil.isBlank(validCode)){
            return AjaxResult.error("验证码不可为空");
        }
        return iExtendUserService.login(httpSession, mobile, validCode);
    }
}
